package com.xiaoyilin.serializer;

import com.xiaoyilin.domain.Company;
import org.apache.kafka.clients.producer.*;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * 不推荐
 *  增加生产者和消费者的耦合度
 * 使用自定义的序列化器
 */
public class Producer {

    public static final String topic = "test";
    public static final String brokerList = "localhost:9092";

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Properties properties = new Properties();
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        //修改值序列化器为自定义序列化器
        properties.put("value.serializer", CompanySerializer.class.getName());
        properties.put("bootstrap.servers", brokerList);
        KafkaProducer<String, Company> kafkaProducer = new KafkaProducer<>(properties);
        Company company  =  Company.builder().name("xiaoyilin").address("成都").build();
        ProducerRecord<String, Company> producerRecord = new ProducerRecord<>("test", company);
        kafkaProducer.send(producerRecord).get();
        kafkaProducer.close();
    }


}
